// pages/recruit/session/index.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag_wx: false,
    flag_find_anchor_call_bodou: true,
    flag_resume_list: true,
    flag_find_anchor_call_bodou_no: true,
    flag_find_anchor_stay_no: true,



    role: wx.getStorageSync('reg_register_role'),
    page: 1,
    local_img: wx.getStorageSync('reg_avatar_url'),
    page: 1,
  },
  //拨电话
  click_anchor_call(e) {
    let anchor_telephone = e.currentTarget.dataset.anchor_telephone;
    this.get_residue_time(1); //留电话需要的波豆
    this.get_phone(); //基础信息
    this.get_is_issue();

    let register_all = this.data.register_all,
      bd = parseInt(register_all.bd),
      call_bd = parseInt(register_all.hf_bd),
      stay_bd = parseInt(register_all.hf_bd2),
      is_linkup = this.data.info.is_linkup,
      is_sign = this.data.info.is_sign;
    if (is_sign==1){
      wx.showToast({
        title: '该主播已签约公会',
        icon: 'none'
      })
    }else{
      if (is_linkup == 1) {
        this.setData({
          flag_resume_list: false
        })
      } else {
        if (call_bd > bd) {
          this.setData({
            flag_find_anchor_call_bodou_no: false
          })
        } else {
          this.setData({
            flag_find_anchor_call_bodou: false
          })
        }
      }
    }
  },
  //留电话
  click_anchor_stay(e) {
    let anchor_telephone = e.currentTarget.dataset.anchor_telephone;
    this.get_residue_time(1); //留电话需要的波豆
    this.get_phone(); //基础信息
    this.get_is_issue();

    let register_all = this.data.register_all,
      bd = parseInt(register_all.bd),
      call_bd = parseInt(register_all.hf_bd),
      stay_bd = parseInt(register_all.hf_bd2),
      is_linkup = this.data.info.is_linkup,
      is_sign = this.data.info.is_sign;
    if (is_sign == 1) {
      wx.showToast({
        title: '该主播已签约公会',
        icon:'none'
      })
    } else {
      if (stay_bd > bd) {
        this.setData({
          flag_find_anchor_call_bodou_no: false
        })
      } else {
        if (is_linkup == 1) {
          //已经沟通
          wx.showToast({
            title: '您已留电话了呢~',
            icon: 'none'
          })
        } else {
          this.setData({
            flag_find_anchor_stay_no: false
          })
        }
      }
    }
  },
  // 已获得权限 拨打电话
  gets_call(e) {
    let is_linkup = this.data.info.is_linkup,
      telephone = this.data.info.anchor_telephone;
    this.setData({
      flag_resume_list:true
    })
    wx.makePhoneCall({
      phoneNumber: telephone,
      success(e) {
        // console.log(e.errMsg == 'makePhoneCall:ok')
      }
    })
  },
  //聊天框内事件
  stay_anchor(e) {
    let resume_id = this.data.info.resume_id,
      is_linkup = this.data.info.is_linkup,
      hf_bd = this.data.register_all.hf_bd,
      hf_bd2 = this.data.register_all.hf_bd2,
      phone = this.data.reg_telephone;
    this.get_residue_time(2, resume_id, phone, hf_bd2);
    this.setData({
      flag_find_anchor_stay_no: true
    })
    wx.showToast({
      title: '操作成功~',
      icon: 'none'
    })
  },
  //点击减少找主播 播豆电话次数 get power
  reduce_gets(e) {
    let resume_id = this.data.info.resume_id,
      is_linkup = this.data.info.is_linkup,
      hf_bd = this.data.register_all.hf_bd;
    if (is_linkup == 1) {
      this.setData({
        flag_resume_list: false
      })
    } else {
      this.reduseBd(resume_id, hf_bd);
      this.link_up_anchor(0, resume_id)
      this.get_residue_time(1);
      this.get_phone();
      this.get_is_issue();
      this.setData({
        flag_resume_list: false
      })
    }
    this.setData({
      flag_find_anchor_call_bodou: true
    })
  },
  go_resume_list() {
    wx.navigateTo({
      url: '../deliveryList/deliveryList?currentData=2'
    })
    this.setData({
      flag_resume_list: true
    })
  },
  //phone_fix
  phone_fix() {
    this.setData({
      focus: true,
    })
  },
  //去充值
  go_recharge() {
    this.setData({
      flag_find_anchor_call_bodou_no: true,
    })
    wx.navigateTo({
      url: '../../payment/bodou/index',
    })
  },
  //扣除播豆
  reduseBd(resume_id, bd_num) {
    wx.request({
      url: app.globalData.url + 'api/recruit/buy/residue/number',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        bd_num: bd_num,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      },
    })
  },
  //会长主动沟通主播
  link_up_anchor(anchor_id, resume_id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/driving/link/up/anchor',
      data: {
        token: app.globalData.token,
        anchor_id: anchor_id,
        resume_id: resume_id,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
      },
    })
  },
  //会长给主播留电话     查看剩余次数 && 留电话
  get_residue_time(is_leave, resume_id, telephone, bd_num) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/recruit/guild/leave/anchor',
      data: {
        token: app.globalData.token,
        is_leave: is_leave,
        resume_id: resume_id,
        telephone: telephone,
        bd_num: bd_num,
      },
      method: 'POST',
      success: function(e) {
        let re_number = e.data.data.re_number;
        that.setData({
          re_number: re_number,
        })
      },
    })
  },
  //默认手机号
  get_phone() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/common/register/info',
      data: {
        token: app.globalData.token,
      },
      method: 'POST',
      success: function(e) {
        // console.log(e.data.data.register_info.telephone)
        let telephone = e.data.data.register_info.telephone;
        that.setData({
          register_all: e.data.data,
          register_info: e.data.data.register_info,
          reg_telephone: telephone
        })
      },
    })
  },
  //输入预留手机号
  inp_find_anchor(e) {
    // console.log(e.detail.value)
    this.setData({
      after_telephone: e.detail.value,
    })
  },
  //加载是否 发布过
  get_is_issue() {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/guild/company/add',
      data: {
        token: app.globalData.token,
        type: "2",
      },
      method: 'POST',
      success: function(e) {
        // console.log(e)
        let is_issue = e.data.data.company.is_issue;
        let residue_num = e.data.data.company.residue_num,
          hf_bd2 = e.data.data.company.hf_bd2,
          hf_bd = e.data.data.company.hf_bd;
        that.setData({
          is_issue: is_issue,
          residue_num: residue_num,
          hf_bd2: hf_bd2,
          hf_bd: hf_bd,
        })
      },
    })
  },
  // 去公司主页
  go_company(e) {
    let token = e.currentTarget.dataset.token;
    wx.navigateTo({
      url: '../../mine/anchorWelfare/checkDetal/checkDetal?token=' + token
    })
  },
  // 去简历
  go_resume(e) {
    console.log(e)
    let resume_id = e.currentTarget.dataset.resume_id,
      chat_id = e.currentTarget.dataset.chat_id;
    if (this.data.role == 2) {
      wx.navigateTo({
        url: '../../resume/singleShow/singleShow?resume_id=' + resume_id
      })
    } else {
      wx.navigateTo({
        url: '../singleRecruit/singleRecruit?resume_id=' + resume_id + '&chat_id=' + chat_id
      })
    }

  },
  none() {},
  //隐藏
  modal_hide() {
    this.setData({
      flag_wx: false,
      flag_find_anchor_call_bodou: true,
      flag_resume_list: true,
      flag_find_anchor_call_bodou_no: true,
      flag_find_anchor_stay_no: true,
    })
  },
  //点击弹框确认
  click_confirm() {
    // console.log(123)
    let _wx = this.data._wx,
      chat_id = this.data.chat_id;
    if (_wx) {
      this.exchange_wx(_wx, chat_id);
      this.setData({
        flag_wx: false,
      })
    } else {
      wx.showToast({
        title: '微信号不能为空',
        icon: 'none',
      })
    }

  },
  // 输入
  inp_wx(e) {
    // console.log(e)
    let _wx = e.detail.value;
    this.setData({
      _wx: _wx,
    })
  },
  //  点击复制微信
  click_copy(e) {
    let _wx = e.currentTarget.dataset.contact_way;
    wx.setClipboardData({
      data: _wx,
      success: function(e) {}
    })
  },
  //投递简历
  send_resume() {
    wx.request({
      url: app.globalData.url + 'api/swop/anchor/send/resume',
      data: {
        token: app.globalData.token,
        chat_id: this.data.chat_id,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.getInfo();
      }
    })
  },
  //会长邀请主播
  click_invite(e) {
    // console.log(e)
    let resume_id = e.currentTarget.dataset.resume_id;
    this.invite(resume_id);
  },
  invite(resume_id) {
    wx.request({
      url: app.globalData.url + 'api/guild/chat/anchor',
      data: {
        token: app.globalData.token,
        resume_id: resume_id,
        type: 2,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.getInfo();
      },
    })
  },
  //拨打电话
  callphone(chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/dial/telephone/resume',
      data: {
        token: app.globalData.token,
        chat_id: chat_id
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
      }
    })
  },
  // 点击拨打电话
  click_call(e) {
    // console.log(e)
    let tel = e.currentTarget.dataset.contact_way,
      chat_id = e.currentTarget.dataset.chat_id;
    this.callphone(chat_id);
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  //交换联系方式
  exchange_tel(telephone, chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/telephone/wechat',
      data: {
        token: app.globalData.token,
        type: 1,
        telephone: telephone,
        chat_id: chat_id,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        this.getInfo();
      }
    })
  },
  exchange_wx(_wx, chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/telephone/wechat',
      data: {
        token: app.globalData.token,
        type: 2,
        wx: _wx,
        chat_id: chat_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e)
        this.getInfo();
      }
    })
  },
  // 点击交换微信
  click_wx(e) {
    let anchor_is_resume = this.data.info.anchor_is_resume;
    // console.log(anchor_is_resume)
    if (anchor_is_resume == 0) {
      if (this.data.role == 1) {
        wx.showToast({
          title: '投递简历后才能交换呢',
          icon: 'none'
        })
      } else if (this.data.role == 2) {
        wx.showToast({
          title: '投递简历后才能交换呢',
          icon: 'none'
        })
      }

    } else {
      console.log('投递成功')
      this.setData({
        flag_wx: true,
      })
      // let tel = wx.getStorageSync('reg_telephone'),
      //   chat_id = e.currentTarget.dataset.chat_id;
      // this.exchange_wx();
    }
  },
  // 点击交换电话
  click_tel(e) {
    let anchor_is_resume = this.data.info.anchor_is_resume;
    // console.log(anchor_is_resume)
    if (anchor_is_resume == 0) {
      if (this.data.role == 1) {
        wx.showToast({
          title: '请先邀请对方投递简历呢~',
          icon: 'none'
        })
      } else if (this.data.role == 2) {
        wx.showToast({
          title: '请先投递简历呢~',
          icon: 'none'
        })
      }
    } else {
      console.log('投递成功');
      let tel = wx.getStorageSync('reg_telephone'),
        chat_id = e.currentTarget.dataset.chat_id;
      this.exchange_tel(tel, chat_id);
    }
  },
  click_resume() {

  },
  //获取会话信息
  getInfo(page) {
    var that = this;
    wx.request({
      url: app.globalData.url + 'api/chat/with/list',
      data: {
        token: app.globalData.token,
        chat_id: this.data.chat_id,
        page: page,
      },
      method: 'POST',
      success: (e) => {
        // console.log(e)
        var timer = setTimeout(() => {
          let list = e.data.data.list.data;
          let info = e.data.data;
          list.reverse();
          list.forEach(function(value, index) {
            value.role = wx.getStorageSync('reg_register_role');
            let nowDate = parseInt(Date.parse(new Date().toLocaleDateString()) / 1000),
              yesterday = nowDate - 86400,
              tomorrow = nowDate + 86400,
              create_time = parseInt(value.create_time);
            if (tomorrow > create_time && create_time > nowDate) {
              //当天
              value.create_time = time.formatTimeTwo(value.create_time, 'h:m');
            } else if (nowDate > create_time && create_time > yesterday) {
              //昨天
              value.create_time = time.formatTimeTwo(value.create_time, '昨天 h:m');
            } else if ((nowDate - create_time > 86400 * 2) && (nowDate - create_time < 86400 * 7)) {
              //超过两天、小于一周
              // that.getWeek(new Date(time.formatTimeTwo(value.create_time, 'Y-M-D')))
              var weekAll = that.getWeek(new Date(time.formatTimeTwo(create_time, 'Y-M-D')))
              value.create_time = time.formatTimeTwo(value.create_time, weekAll + ' h:m');
              // value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D');
            } else {
              //大于一周
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            }
            // create_time = time.formatTimeTwo(create_time, 'Y-M-D h:m');
          })
          info.anchor_avatar = info.anchor_avatar ? info.anchor_avatar : 'https://www.zhibohome.net/images/default.png';
          info.guild_avatar = info.guild_avatar ? info.guild_avatar : 'https://www.zhibohome.net/images/default.png';
          console.log(list)
          that.setData({
            list: list,
            info: info,
          })
          // clearInterval(timer)
        }, 100)

      }
    })
  },
  //得到星期
  getWeek(date) {
    var week;
    if (date.getDay() == 0) week = "星期日"
    if (date.getDay() == 1) week = "星期一"
    if (date.getDay() == 2) week = "星期二"
    if (date.getDay() == 3) week = "星期三"
    if (date.getDay() == 4) week = "星期四"
    if (date.getDay() == 5) week = "星期五"
    if (date.getDay() == 6) week = "星期六"
    return week;
  },
  // 已读
  readed(chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/message/seting/read',
      data: {
        token: app.globalData.token,
        chat_id: chat_id,
      },
      method: 'POST',
      responseType: 'text',
      success: (e) => {
        // console.log(e)
      }
    })
  },
  // 获取容器高度，使页面滚动到容器底部
  pageScrollToBottom: function() {
    wx.createSelectorQuery().select('.wrap').boundingClientRect(function(rect) {
      // 使页面滚动到底部
      let timer = setTimeout(function() {
        wx.pageScrollTo({
          scrollTop: rect.bottom + 5000,
          duration: 0
        })
        clearTimeout(timer)
      }, 300)

      console.log(rect.bottom)
    }).exec()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let chat_id = options.chat_id,
      title = options.title;
    wx.setNavigationBarTitle({
      title: title //页面标题为路由参数
    })

    this.setData({
      chat_id: chat_id,
    })
    // console.log(chat_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.pageScrollToBottom();
    this.get_residue_time(1); //留电话需要的波豆
    this.get_phone(); //基础信息
    this.get_is_issue();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      role: wx.getStorageSync('reg_register_role')
    })
    this.getInfo(this.data.page);
    this.readed(this.data.chat_id);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    // wx.request({
    //   url: '',
    //   data: {},
    //   method: 'GET',
    //   success: function(res) {},
    //   fail: function(res) {},
    //   complete: function(res) {
    //     wx.stopPullDownRefresh();
    //   }
    // })
    let that = this;
    let page = this.data.page;
    page = page + 1;
    that.setData({
      page: page
    })
    this.list();
  },
  // 加载
  list: function(e) {
    let that = this;
    let page = this.data.page
    that.get_push(page);
    wx.showLoading({
      icon: 'none',
    })
  },
  //获取推送列表
  get_push(page) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/chat/with/list',
      data: {
        token: app.globalData.token,
        chat_id: this.data.chat_id,
        page: page,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        //普通情况
        if (e.data.data.list.current_page > e.data.data.list.last_page) {
          // console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          // console.log("没到底");          
          if (that.data.page == "1") {
            let list_old = e.data.data.list.data;
            list_old.reverse();
            list_old.forEach(function(value, index) {
              value.role = wx.getStorageSync('reg_register_role');
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            // list_old.forEach(function(value, index) {
            //   value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            // })
            that.setData({
              list: list_old,
            })
          } else {
            // console.log("不不不不不");
            let list = that.data.list;
            let list_old = e.data.data.list.data;
            list_old.reverse();
            list_old.forEach(function(value, index) {
              value.role = wx.getStorageSync('reg_register_role');
              value.create_time = time.formatTimeTwo(value.create_time, 'Y-M-D h:m');
            })
            // list_old.forEach(function(value, index) {
            //   value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            // })
            list.unshift(...list_old)
            // console.log(recruit_info);
            that.setData({
              list: list,
            })
          }
        }
        // console.log('page+' + that.data.page)
        wx.hideLoading();
        wx.stopPullDownRefresh();
        console.log(that.data.list)
      },
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})